package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/5/15 14:38
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    @Override
    public ServiceVO saveDamageListGoods(DamageList damageList, String damageListGoodsStr, String damageNumber) {

        try {
            damageListGoodsDao.saveDamageList(damageList);
            // 创建一个gson对象
            Gson gson = new Gson();
            //把JSON格式的字符串转为List
            List<DamageListGoods> damageListGoods = gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>(){}.getType());
            damageListGoodsDao.saveDamageListGoods(damageListGoods);
        } catch (JsonSyntaxException e) {
            return new ServiceVO(400, "保存失败！");
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> listDamageListGoods(String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        List<DamageList> damageLists = damageListGoodsDao.listDamageListGoods(sTime,eTime);
        map.put("rows",damageLists);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        // 创建一个map对象
        HashMap<String, Object> map = new HashMap<>();
        // 根据Id查询报损单商品信息
        List<DamageListGoods> damageLists = damageListGoodsDao.goodsList(damageListId);
        map.put("rows",damageLists);
        return map;
    }
}
